﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using syc.Helper;
using System.Drawing;
namespace syc.Sale
{
    public partial class 原料到货日报 : System.Web.UI.Page
    {
        protected DateTime d1, d2, d3, d4, n;//d1月初日期，d2上旬截止日期,d3中旬截止日期,d4月末日期,n年初日期
        protected DateTime 旬截止日期;
        protected DateTime 上旬截止日期;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               // Button_Query_Click(null, null);
                this.Year.SelectedValue = DateTime.Today.Year.ToString();
                this.Month.SelectedValue = DateTime.Today.Month.ToString();
                 RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
            }
            GetData(this.Year.SelectedValue, this.Month.SelectedValue);
            this.SqlDataSource1.SelectCommand = "select  主材名称,单位,名称,isnull(dbo.当月累计(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + DateTime.Parse(RiQi.Text) + "'),0) as 当日到货量,isnull(dbo.当月累计(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + d1 + "'),0) as 当月累计量,isnull(dbo.当月合同量(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + n + "'),0) as 全年合同量,isnull(dbo.当月合同量(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + d1 + "'),0) as 当月合同量,isnull(dbo.当月累计(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + n + "'),0) as 当年累计量 from 主要原材料到货日记录 where 主材名称='油管光管' or 主材名称='套管光管' group by 主材名称,名称,单位,dbo.当月累计(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + DateTime.Parse(RiQi.Text) + "'),dbo.当月累计(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + d1 + "'),dbo.当月合同量(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + n + "'),dbo.当月合同量(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + d1 + "'),dbo.当月累计(主材名称,名称,'" + DateTime.Parse(RiQi.Text) + "','" + n + "')";
                   
            GridViewHelper helper = new GridViewHelper(this.GridView1);
            helper.RegisterGroup("主材名称", true, true);
            helper.RegisterSummary("当日到货量", SummaryOperation.Sum, "主材名称");
            helper.RegisterSummary("当月累计量", SummaryOperation.Sum, "主材名称");
            helper.RegisterSummary("全年合同量", SummaryOperation.Sum, "主材名称");
            helper.RegisterSummary("当月合同量", SummaryOperation.Sum, "主材名称");
            helper.RegisterSummary("当年累计量", SummaryOperation.Sum, "主材名称");
            helper.GroupHeader += new GroupEvent(helper_GroupHeader);
            helper.GroupSummary += new GroupEvent(helper_GroupSummary);
            helper.GeneralSummary += new FooterEvent(helper_GeneralSummary);
            helper.ApplyGroupSort();
            this.GridView1.DataBind();
        }
        //自定义分组标题样式
        private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            row.Cells[0].Height = 33;
            row.BackColor = Color.LightGray;
            row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;

        }

        //自定义合计标题样式
        private void helper_GeneralSummary(GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.BackColor = Color.LightSlateGray;
            row.Cells[0].Text = "合计:";
            row.Height = 20;
        }


        //自定义合计标题样式
        private void helper_GroupSummary(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.Cells[0].Text = "合计:";
            row.Height = 20;
        }
        public void GetData(string yy,string mm)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select 月初日期,上旬日期,中旬日期,月末日期,年初=(select 月初日期 from 供应处生产月设置 where 年度='" + yy + "' and 月份=1) from 供应处生产月设置 where 年度='" + yy + "' and 月份='" + mm + "'", Conn);
            sda.Fill(ds);
            try
            {
                d1 = DateTime.Parse(ds.Tables[0].Rows[0][0].ToString());
                d2 = DateTime.Parse(ds.Tables[0].Rows[0][1].ToString());
                d3 = DateTime.Parse(ds.Tables[0].Rows[0][2].ToString());
                d4 = DateTime.Parse(ds.Tables[0].Rows[0][3].ToString());
                n = DateTime.Parse(ds.Tables[0].Rows[0][4].ToString());
              
               
            }
            catch
            {
                this.Message.Text = ".该扎帐月没有设置";
                this.SqlDataSource1.SelectCommand = "";
                this.GridView1.DataBind();
            }
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            GetData(this.Year.SelectedValue, this.Month.SelectedValue);
        }

      
       
    }
}

